package helper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYValueSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import simpleDB.DatabaseHelper;
import simpleDB.StatisticDataObject;
import activities.StatisticsActivity.ViewState;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;

public class GraphDataWrapper {

	public XYMultipleSeriesDataset _dataset;
	public XYMultipleSeriesRenderer _renderer;
	public ViewState _viewState;
	public TimeSpan _timeSpan;

	public HashMap<Integer, String> dayMapping = new HashMap<Integer, String>();
	public HashMap<Integer, String> monthMapping = new HashMap<Integer, String>();

	public DatabaseHelper dbHelper;

	public double yAxisMin = 0;
	public double xAxisMin = 1;
	public double xAxisMax = 0;
	public double yAxisMax = 0;

	public GraphDataWrapper(Context c, ViewState v, TimeSpan ts) {
		dbHelper = new DatabaseHelper(c);
		_viewState = v;
		_timeSpan = ts;
		dayMapping.put(1, "Mon");
		dayMapping.put(2, "Tue");
		dayMapping.put(3, "Wed");
		dayMapping.put(4, "Thu");
		dayMapping.put(5, "Fri");
		dayMapping.put(6, "Sat");
		dayMapping.put(7, "Sun");

		monthMapping.put(1, "Jan");
		monthMapping.put(2, "Feb");
		monthMapping.put(3, "Mar");
		monthMapping.put(4, "Apr");
		monthMapping.put(5, "May");
		monthMapping.put(6, "June");
		monthMapping.put(7, "July");
		monthMapping.put(8, "Aug");
		monthMapping.put(9, "Sept");
		monthMapping.put(10, "Oct");
		monthMapping.put(11, "Nov");
		monthMapping.put(12, "Dec");

	}

	public void setViewState(ViewState v) {
		this._viewState = v;

	}

	public XYMultipleSeriesDataset getDataset() {
		return this._dataset;
	}

	public XYMultipleSeriesRenderer getRenderer() {
		return this._renderer;
	}

	public void updateSingleRendererStyling() {
		_renderer = new XYMultipleSeriesRenderer();
		RendererFactory rf = new RendererFactory();
		XYSeriesRenderer tmp = rf.createRendererFactory(1);
		
		tmp.setGradientEnabled(false);
		

		_renderer.addSeriesRenderer(tmp);
		_renderer.clearXTextLabels();
		_renderer.clearYTextLabels();
		_renderer.setXLabels(0);
		

		if (_viewState == ViewState.Day) {
			_renderer.setXTitle("Day");
			/*
			 * this is needed, so that the engine automatically creates the
			 * "best" view displaying all bars*
			 */
			_renderer.setYAxisMin(yAxisMin);
			_renderer.setYAxisMax(yAxisMax);
			_renderer.setXAxisMax(xAxisMax);
			_renderer
					.setRange(new double[] { 0.4, 7.6, yAxisMin, yAxisMax + 1 });

			_renderer.setChartTitle("Period: "
					+ new SimpleDateFormat("dd. MMMM")
							.format(this._timeSpan.startDate)
					+ " - "
					+ new SimpleDateFormat("dd. MMMM")
							.format(this._timeSpan.endDate));

			

			for (int i = 0; i <= yAxisMax; i = i + 10) {
				_renderer.addYTextLabel(i, String.valueOf(i));

			}

			for (int i = 1; i <= xAxisMax; i++) {
				_renderer.addXTextLabel(i, dayMapping.get(i));

			}

		}

		if (_viewState == ViewState.Month) {
			_renderer.setXTitle("Month");
			/*
			 * this is needed, so that the engine automatically creates the
			 * "best" view displaying all bars*
			 */
			xAxisMax = 12;
			_renderer.setYAxisMin(yAxisMin);
			_renderer.setYAxisMax(yAxisMax);
			_renderer.setXAxisMax(xAxisMax);
			_renderer.setRange(new double[] { 0.4, 12.4, yAxisMin,
					yAxisMax + 10 });

			_renderer.setChartTitle("Period: "
					+ new SimpleDateFormat("yyyy")
							.format(this._timeSpan.startDate));

			
			for (int i = 1; i <= 12; i++) {
				_renderer.addXTextLabel(i, monthMapping.get(i));

			}

		}
		_renderer.setBarSpacing(0.1f);
		_renderer.setXRoundedLabels(false);

		_renderer.setYTitle("Number of repetitions");

		_renderer.setMargins(new int[] { 100, 100, 10, 0 });
		_renderer.setChartValuesTextSize(20);

		_renderer.setAxesColor(Color.WHITE);
		_renderer.setAxisTitleTextSize(30);
		_renderer.setShowLegend(false);
		_renderer.setChartTitleTextSize(35);
		_renderer.setLabelsTextSize(20);
		_renderer.setLegendTextSize(25);
		_renderer.setPanEnabled(false, false);
		_renderer.setLabelsColor(Color.WHITE);
		_renderer.setYLabelsAlign(Align.RIGHT);
		_renderer.setInScroll(true);
		_renderer.setApplyBackgroundColor(true);
		_renderer.setBackgroundColor(Color.argb(0x00, 0x01, 0x01, 0x01));
		_renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));

		_renderer.setZoomEnabled(false, false);

	}

	public void updateSingleDataset(int exId, int persId, ViewState viewState,
			TimeSpan currentTimeSpan, String exString) {
		_timeSpan = currentTimeSpan;
		_dataset = new XYMultipleSeriesDataset();
		this._viewState = viewState;

		XYValueSeries tmpSeries = new XYValueSeries(exString);
		reInitMaxValues();

		if (viewState == ViewState.Day) {
			// since days are shown, max is 7
			this.xAxisMax = 7;

			Calendar c = Calendar.getInstance();
			c.setTime(currentTimeSpan.startDate);
			for (int j = 0; j < 7; j++) {
				tmpSeries.add(j + 1, dbHelper.getRepetitions(persId,
						new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()),
						exId).repetitions);

				c.add(Calendar.DATE, 1);
			}
			c.setTime(currentTimeSpan.startDate);
		}
		if (this._viewState == ViewState.Month) {
			this.xAxisMax = 12;
			Calendar c = Calendar.getInstance();
			c.setTime(currentTimeSpan.startDate);
			ArrayList<StatisticDataObject> monthsRepsMapping = dbHelper
					.getRepetionsPerMonth(persId, Integer
							.valueOf((new SimpleDateFormat("yyyy").format(c
									.getTime()))), exId);
			for (int j = 1; j < 13; j++) {
				tmpSeries.add(j, monthsRepsMapping.get(j).repetitions);

			}
		}
		if (viewState == ViewState.Week) {

		}
		this.yAxisMax = tmpSeries.getMaxY();

		this._dataset.addSeries(tmpSeries);
	}

	private void reInitMaxValues() {
		yAxisMin = 0;
		xAxisMin = 1;
		xAxisMax = 0;
		yAxisMax = 0;
	}

	public void updateTimeSpan(boolean b) {
		
	}

	public void setColor(String name) {
		if(name.equals("Squats")){
		this._renderer.getSeriesRendererAt(0).setColor(Settings.getInstance().getExerciseColor("SQUATS"));
		}else if(name.equals("Biceps Curls")){
			this._renderer.getSeriesRendererAt(0).setColor(Settings.getInstance().getExerciseColor("BICEP-CURLs"));
		}else if(name.equals("Sit ups")){
			this._renderer.getSeriesRendererAt(0).setColor(Settings.getInstance().getExerciseColor("SIT-UPs"));
		}
		
	}
}
